define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    

    var helpData, settingsData, applicationData;

    $.widget('rk.foldSubMenuCtrl', {
        options: {
            top: 0,
            left: 0,
            botton: 0,
            right: 0,
            title: '',
            id: '',
            tpl: 'normal'
        },
        _create: function() {
            var me = this;
            var elem = me.element,
                opt = me.options;

            if(!applicationData && opt.applicationData){
                applicationData = opt.applicationData;
            }

            if (!helpData)
                helpData = [{
                    "class_li": "p-set",
                    "class_i": "new-guide",
                    "link": "new_guide",
                    "name": rk.i18n('PAGE_MENU_GUIDE'),
                    "target": "_self",
                    "id": Math.random().toString(36).substr(2),
                    "click": function() {
                        require.async('page/js/frame/userGuider/userGuiderCtrl', function() {
                            $(window).userGuiderCtrl();
                        });
                    }
                }, {
                    "class_li": "",
                    "class_i": "m-download",
                    "link": "http://www.xiaoshouyi.com/download/",
                    "name": rk.i18n('PAGE_MENU_APP_DOWNLOAD'),
                    "target": "_blank",
                    "click": ""
                }, {
                    "class_li": "",
                    "class_i": "i-rule",
                    "link": "/score.action",
                    "name": rk.i18n('PAGE_MENU_SCORE'),
                    "target": "_self",
                    "click": ""
                }, {
                    "class_li": "",
                    "class_i": "p-help",
                    "link": "http://www.xiaoshouyi.com/helpcenter/",
                    "name": rk.i18n('PAGE_MENU_HELP_CENTER'),
                    "target": "_blank",
                    "click": ""
                }, {
                    "class_li": "",
                    "class_i": "p-video",
                    "link": "/",
                    "name": rk.i18n('PAGE_MENU_HELP_VADIO'),
                    "target": "_self",
                    "id": Math.random().toString(36).substr(2),
                    "click": function() {
                        popupHelpvideo();
                    }
                }, {
                    "class_li": "",
                    "class_i": "p-message",
                    "link": "/feedback.action",
                    "name": rk.i18n('PAGE_MENU_FEEDBACK'),
                    "target": "_blank",
                    "click": ""
                    //"id": Math.random().toString(36).substr(2),
                    /*"click": function() {
                        popupFeedback();
                    }*/
                }];
            if (!settingsData)
                settingsData = [{
                    "class_li": "p-set",
                    "link": "",
                    "class_i": "p-log",
                    "name": rk.i18n('PAGE_MENU_LOG'),
                    "target": "_self",
                    "id": Math.random().toString(36).substr(2),
                    "click": function() {
                        rk.post('/global/open-log-window.action', {
                            decorator: 'ajaxformat',
                            confirm: true
                        }).done(function(data) {
                            var succhtml = require('../settings/logSetting.tpl')
                            rk.dialog({
                                title: rk.i18n('PAGE_MENU_LOG'),
                                success: function(contentElem, footerElem) {
                                    if(data.data.createdAt != 0) {
                                        data.data.createdAt = Globalize.format(new Date(data.data.createdAt - 0), 'yyyy-MM-dd HH:mm:ss');
                                    } else {
                                        data.data.createAt = null;
                                    }
                                    $('#dialog').html(rk.templateText(succhtml, data));

                                    footerElem.find('[act="cancel"]').click(function() {
                                        $('#dialog').dialog('close');
                                    });
                                },
                                footer: [{
                                    act: 'cancel',
                                    visible: true,
                                    text: '我知道了'
                                }]
                            });
                        });
                    }
                }, {
                    "class_li": "p-set",
                    "link": "/",
                    "class_i": "logo-set",
                    "name": rk.i18n('PAGE_MENU_LOGO'),
                    "target": "_self",
                    "id": Math.random().toString(36).substr(2),
                    "click": function() {
                        rk.dialog({
                            title: rk.i18n('PAGE_MENU_LOGO'),
                            success: function(contentElem, footerElem) {
                                contentElem.logoSetting();
                            },
                            footer: [{
                                act: 'cancel',
                                visible: true,
                                color: 'white',
                                text: '取消'
                            }, {
                                act: 'save',
                                visible: true,
                                text: '保存设置'
                            }]
                        });
                    }
                }, {
                    "class_li": "p-set",
                    "link": "/",
                    "class_i": "theme-set",
                    "name": rk.i18n('PAGE_MENU_THEME'),
                    "target": "_self",
                    "id": Math.random().toString(36).substr(2),
                    "click": function() {
                        rk.dialog({
                            title: rk.i18n('PAGE_MENU_THEME'),
                            success: function(contentElem, footerElem) {
                                contentElem.themeSetting();
                            },
                            footer: [{
                                act: 'cancel',
                                visible: true,
                                color: 'white',
                                text: '取消'
                            }, {
                                act: 'save',
                                visible: true,
                                text: '保存设置'
                            }]
                        });
                    }
                }, {
                    "class_li": "p-set",
                    "link": "/usersetting.action",
                    "class_i": "p-set",
                    "name": rk.i18n('PAGE_MENU_USER_CONFIG'),
                    "id": Math.random().toString(36).substr(2),
                    "target": "_self",
                    "click": ""
                }, {
                    "class_li": "",
                    "link": "/admin/admin-home/index.action",
                    "class_i": "s-set",
                    "name": rk.i18n('PAGE_MENU_SYS_CONFIG'),
                    "id": Math.random().toString(36).substr(2),
                    "target": "_blank",
                    "click": ""
                }, {
                    "class_li": "",
                    "link": "/global/logout-system.action",
                    "class_i": "s-exit",
                    "name": rk.i18n('PAGE_MENU_LOGOUT'),
                    "id": Math.random().toString(36).substr(2),
                    "target": "_self",
                    "click": ""
                }];

        },
        close_timer: null,
        _init: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var leftside = $('#pageleft');

            var itemsTpl, itemsHtml;

            if (opt.tpl === 'normal') {
                //菜单收起状态，CRM,常用，最近访问浮出的子菜单
                itemsTpl = require('../../../tmpl/frame/fold_menu.tpl');
                itemsHtml = rk.templateText(itemsTpl);
                leftside.append(itemsHtml);

                leftside.find('.menu-thumbnail-box:last').css('top', opt.top);
                leftside.find('.menu-thumbnail-box:last > .menu-thumbnail-content > .menu-thumbnail-header > h5').html(opt.title);
                leftside.find('.menu-thumbnail-box:last').attr('id', opt.id);

                if (opt.id === 'fold_crm_menu') {
                    leftside.find('#fold_crm_menu > .menu-thumbnail-content > .menu-thumbnail-body').beScroll();
                }

            } else if (opt.tpl === 'set') {
                //菜单收起状态，个人设置，帮助浮出的子菜单
                itemsTpl = require('../../../tmpl/frame/fold_set_menu.tpl');
                itemsHtml = rk.templateText(itemsTpl);
                leftside.append(itemsHtml);

                leftside.find('.set-thumbnail-box:last').css('bottom', opt.bottom);
                leftside.find('.set-thumbnail-box:last').attr('id', opt.id);

                me._buildSetMenu(opt.title);
            } else if (opt.tpl == 'unfold_set') {
                //菜单展开状态，个人设置，帮助浮出的子菜单
                itemsTpl = require('../../../tmpl/frame/unfold_set_menu.tpl');
                itemsHtml = rk.templateText(itemsTpl);

                var setpop = $('.set-pop');
                setpop.append(itemsHtml);

                setpop.find('.profile-drop-div:last').css('left', opt.left);
                setpop.find('.profile-drop-div:last').attr('id', opt.id);

                me._buildUnFoldSetMenu(opt.title);
            }

            elem.rkEasyMenu({
                menuId: opt.id
            });
        },
        closeMenu: function() {
            $('.menu-thumbnail-box').hide();
            $('.set-thumbnail-box').hide();
            $('.profile-drop-div').hide();
        },
        appendMenu: function(type, item) {
            var me = this;
            var leftside = $('#pageleft');
            var menuList = leftside.find('#' + type).find('.menu-thumbnail-list');
            if (type === 'fold_history_menu') {
                menuList.append('<li><a href="' + item.link + '" target="_blank"><span class="ico ' + item.className + '"></span>' + rk.htmlEscape(item.name) + '</a></li>');
            } else {
                menuList.append('<li><a href="' + item.link + '">' + rk.htmlEscape(item.name) + '</a></li>');
            }
        },
        buildMenu: function(type, data) {
            var me = this;
            var leftside = $('#pageleft');
            var menuList = leftside.find('#' + type).find('.menu-thumbnail-list');
            if (data.length > 0) {

                menuList.html('');

                for (var i = 0; i < data.length; i++) {
                    me.appendMenu(type, data[i]);
                }
            }
        },
        _buildSetMenu: function(type) {
            var me = this;
            var leftside = $('#pageleft');
            var menuList;
            if (type === 'help') {
                menuList = leftside.find('#fold_help_menu > .set-thumbnail-content >.profile-drop-div >.profile-drop-menu');
                me._renderSetMenu(menuList, helpData);
            } else if(type == 'application'){
                menuList = leftside.find('#fold_application_menu > .set-thumbnail-content >.profile-drop-div >.profile-drop-menu');
                menuList.attr("class","profile-drop-menu profile-platform-drop-menu");
                me._renderSetMenu(menuList, applicationData);
            } else {
                menuList = leftside.find('#fold_setting_menu >.set-thumbnail-content >.profile-drop-div >.profile-drop-menu');
                me._renderSetMenu(menuList, settingsData);
            }
        },
        _buildUnFoldSetMenu: function(type) {
            var me = this;
            var setpop = $('.set-pop');
            var menuList;
            if (type === 'help') {
                menuList = setpop.find('#unfold_help_menu > .profile-drop-menu');
                me._renderSetMenu(menuList, helpData);
            } else if(type == 'application'){
                menuList = setpop.find('#unfold_application_menu > .profile-drop-menu');
                menuList.attr("class","profile-drop-menu profile-platform-drop-menu");
                menuList.parent().css("max-height","230px");
                menuList.parent().beScroll();
                me._renderSetMenu(menuList, applicationData);
            } else {
                menuList = setpop.find('#unfold_setting_menu > .profile-drop-menu');
                me._renderSetMenu(menuList, settingsData);
            }
        },
        _renderSetMenu: function(container, data) {
            for (var i = 0; i < data.length; i++) {
                var item = data[i];
                var isAdminItem = false;
                if((item.name === rk.i18n('PAGE_MENU_SYS_CONFIG')) || 
                   (item.name === rk.i18n('PAGE_MENU_LOGO'))) isAdminItem = true;
                if (!isAdminItem || (isAdminItem && window.SESSION.isXsyCrmAdmin))
                if (item.click !== '') {
                    container.append('<li class="' + item.class_li + '"><a href="javascript:;" refId="' + item.id + '"><i class="' + item.class_i + '"></i>' + item.name + '</a></li>');
                } else {
                    container.append('<li class="' + item.class_li + '"><a href="' + item.link + '" target="' + item.target + '"> <i class="' + item.class_i + '"></i>' + item.name + '</a></li>');
                }
            }
            container.on('click', 'a[refId]', function() {
                var me = this;
                var target = $(this);
                for (var i = 0; i < helpData.length; i++) {
                    if (helpData[i].id === target.attr('refId')) {
                        (helpData[i].click)();
                    }
                }
                for (var j = 0; j < settingsData.length; j++) {
                    if (settingsData[j].id === target.attr('refId')) {
                        (settingsData[j].click)();
                    }
                }
                for (var k = 0; k < applicationData.length; k++) {
                    if (applicationData[k].id === target.attr('refId')) {
                        (applicationData[k].click)(applicationData[k].id);
                    }
                }
            });
        }
    });

    var popupFeedback = function() {
        rk.dialog({
            title: rk.i18n('PAGE_MENU_FEEDBACK'),
            width: 630,
            success: function(contentElem, footerElem) {
                footerElem.find('a[act="submit"]').click(function() {
                    var submitText = contentElem.find('textarea').val();
                    if (!submitText) {
                        rk.noticeError(rk.i18n('PAGE_MENU_PLS_COMPOSE_FEEDBACK'));
                        return;
                    }
                    rk.post('/feedback/add.action', {
                        decorator: 'ajaxformat',
                        confirm: true,
                        content: submitText
                    }).done(function() {
                        var succhtml = require('../../../tmpl/tpl_feedback_succ.tpl')
                        rk.noticeSuccess(rk.i18n('PAGE_MENU_THANKS_FOR_FEEDBACK'));
                        $('#dialog').html(rk.templateText(succhtml));
                        footerElem.remove();
                    });
                });
            },
            html: rk.templateText(require('../../../tmpl/tpl_feedback_popup.tpl')),
            footer: [{
                act: 'submit',
                visible: true,
                text: rk.i18n('SEND') //'发送'
            }]
        })
    };
    var popupHelpvideo = function() {
        require.async('page/tmpl/helpvideo/helpvideo.tpl', function(tmpl) {
            rk.dialog({
                title: rk.i18n('PAGE_MENU_USE_HELP_VADIO'),
                width: 550,
                height: 200,
                success: function(contentElem, footerElem) {

                },
                html: rk.templateText(tmpl),
                footer: false
            })
        });
    }
});
